/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.shsc.facade;

import com.bionic.shsc.dao.interfaces.ITestDAO;
import com.bionic.shsc.dao.interfaces.IProfileDAO;
import com.bionic.shsc.dao.interfaces.IUserDAO;
import com.bionic.shsc.db.DB;
import com.bionic.shsc.entities.ProfileData;
import com.bionic.shsc.entities.TestData;
import com.bionic.shsc.entities.UserData;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ant Class contains data and methods of "user" business object
 */
public class UserFacade {

    public UserData user;
    private IUserDAO userDB;
    private ProfileData profile;
    private IProfileDAO profileDB;
    //
    List<TestData> tests;
    private ITestDAO testDB;

    public UserFacade() {
        tests = new ArrayList<TestData>();
    }

    /**
     * @return the user
     */
    public UserData getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(UserData user) {
        this.user = user;
    }

    /**
     * @return the userDB
     */
    public IUserDAO getUserDB() {
        if (userDB == null) {
            userDB = DB.getInstance().getFactory().getUserDAO();
        }
        return userDB;
    }

    /**
     * @return the profile
     */
    public ProfileData getProfile() {
        return profile;
    }

    /**
     * @param profile the profile to set
     */
    public void setProfile(ProfileData profile) {
        this.profile = profile;
    }

    /**
     * @return the profileDB
     */
    public IProfileDAO getProfileDB() {
        if (profileDB == null) {
            profileDB = DB.getInstance().getFactory().getProfileDAO();
        }
        return profileDB;
    }

    public int loginExecute(String login, String password) {
        int userID = getUserDB().isLoginExecute(login, password);
        if (userID >= 0) {
            profile = getProfileDB().findProfile(userID);
            fillTestList();
            return userID;
        } else {
            return -1000;
        }

    }

    public ITestDAO getTestDB() {
        if (testDB == null) {
            testDB = DB.getInstance().getFactory().getTestDAO();
        }
        return testDB;
    }

    private void fillTestList() {
        tests = getTestDB().getList();
    }

    public List<TestData> getTests() {
        if (tests.isEmpty()) {
            tests = getTestDB().getList();
        }
        return tests;
    }

    public boolean registerUser(String fullname, String email, String passhash,
            String street, String houseNum, String zip) {
        try {
            UserData userData = new UserData();
            userData.setEmail(email);
            userData.setPass(passhash);
            if (getUserDB().registerUser(userData)) {
                // get userID
                int userID = getUserDB().findUserByEmail(userData.getEmail());

                if (userID >= 0) {
                    ProfileData profile = new ProfileData();
                    profile.setUser_id(userID);
                    profile.setFullname(fullname);
                    profile.getAddress().setStreet(street);
                    profile.getAddress().setHouseNum(houseNum);
                    profile.getAddress().setZip(zip);
                    getProfileDB().createProfile(profile);
                }
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            Logger.getLogger(UserFacade.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }

    }
}
